//
//  SearchVC.m
//  SeaChat
//
//  Created by 刘汉 on 16/6/16.
//  Copyright © 2016年 潘强. All rights reserved.
//

#import "SearchVC.h"

@interface SearchVC ()<EMClientDelegate,UITextFieldDelegate,EMContactManagerDelegate>
@property (retain, nonatomic) IBOutlet UITextField *SearchText;
@property (nonatomic,retain)UILabel *userNameLabel;
@property (nonatomic,retain)UIView *buddyV;
@property(nonatomic,assign)CGRect viewRect;
@property (retain, nonatomic) IBOutlet UIButton *addFriend;

@end

@implementation SearchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [[EMClient sharedClient] addDelegate:self delegateQueue:nil];
    [[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];

    self.view.backgroundColor = [UIColor whiteColor];

    self.SearchText = [UITextField new];
    [self.view addSubview:self.SearchText];
    [self.SearchText makeConstraints:^(MASConstraintMaker *make) {

        make.top.equalTo(69);
        make.left.equalTo(15);
        make.right.equalTo(-15);
        make.bottom.equalTo(-570);
    }];
    self.SearchText.layer.borderWidth = 1;
    self.SearchText.layer.borderColor = [[UIColor lightGrayColor]CGColor];
    self.SearchText.layer.masksToBounds = YES;
    self.SearchText.layer.cornerRadius = 10;
    self.SearchText.delegate = self;
    [self.SearchText addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];


    self.buddyV = [UIView new];
    [self.view addSubview:self.buddyV];
    [self.buddyV makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.SearchText.bottom);
        make.left.right.equalTo(self.SearchText);
        make.height.equalTo(50);
    }];


    self.userNameLabel = [UILabel new];
    [self.buddyV addSubview:self.userNameLabel];
    [self.userNameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.buddyV.top);
        make.width.equalTo(self.buddyV).multipliedBy(0.7);
        make.height.equalTo(self.buddyV);
        make.left.equalTo(self.SearchText);
    }];


    self.userNameLabel.backgroundColor = [UIColor clearColor];
    self.addFriend = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buddyV addSubview:self.addFriend];
    [self.addFriend makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.buddyV);
        make.left.equalTo(self.userNameLabel.right).offset(5);
        make.right.equalTo(self.buddyV);
        make.height.equalTo(self.buddyV).multipliedBy(0.7);
    }];
    self.addFriend.backgroundColor = [UIColor lightGrayColor];
    [self.addFriend setTitle:@"添加好友" forState:UIControlStateNormal];
    [self.addFriend addTarget:self action:@selector(SearchAction:) forControlEvents:UIControlEventTouchUpInside];
    self.addFriend.layer.masksToBounds = YES;
    self.addFriend.layer.cornerRadius = 10;

    self.buddyV.hidden = YES;
    self.addFriend.hidden =YES;


}
-(void)textFieldChanged:(UITextField *)textfield{
    self.userNameLabel.text = textfield.text;


}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.SearchText resignFirstResponder];

    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{

    self.buddyV.hidden = NO;
    self.addFriend.hidden = NO;
}


- (IBAction)SearchAction:(id)sender {
//获取要添加好友的名字
    //向服务器发送请求
    //message 请求添加好友的额外信息
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"给好友留言" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

    }];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *message = alertController.textFields.firstObject.text;
        [[EMClient sharedClient].contactManager addContact:self.SearchText.text message:message];
        NSLog(@"%@",message);
        [SVProgressHUD setBackgroundColor:[UIColor lightGrayColor]];
        [SVProgressHUD setMinimumDismissTimeInterval:1.5];
        [SVProgressHUD showSuccessWithStatus:@"好友请求已发送"];

    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {


    }]];

    [self presentViewController:alertController animated:YES completion:^{


    }];


}


- (void)dealloc {
    [_SearchText release];
    [_addFriend release];
    [_userNameLabel release];
    [_buddyV release];
    [super dealloc];
#warning 代理放在对话控制器里面比较好;
    [[EMClient sharedClient] removeDelegate:self];
    [[EMClient sharedClient].contactManager removeDelegate:self];
}
@end
